package com.woniu.humanresource.controller;


import com.woniu.humanresource.entity.Departure;
import com.woniu.humanresource.entity.DepartureImg;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.DepartureService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class DepartureController {
    @Value("${upload.addResourceLocation}")
    private String addResourceLocation;
    @Resource
    DepartureService departureService;
    //分页查询离职信息
    @PostMapping("/departure/{pageIndex}/{pageSize}")
    public ResponseText<PageBean<Departure>> getDepartures(@PathVariable("pageIndex") int page,
                                                           @PathVariable("pageSize") int pageSize,
                                                           @RequestBody Departure departure){
        ResponseText<PageBean<Departure>> responseText=null;
        try {
            PageBean<Departure> pageBean = departureService.selectCondition(departure, page, pageSize);
            responseText=new ResponseText<PageBean<Departure>>(pageBean,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
//添加离职信息
    @PostMapping("/departure")
    public ResponseText addDeparture(@RequestBody Departure departure){
        ResponseText responseText=null;
        try {
            departureService.insertDeparture(departure);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
//修改离职信息
    @PutMapping("/departure")
    public ResponseText updateDeparture(@RequestBody Departure departure){
        ResponseText responseText=null;
        try {
            departureService.updateDeparture(departure);
            responseText=new ResponseText<PageBean<Departure>>(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
//删除离职信息
    @DeleteMapping("/departure/{id}")
    public ResponseText deleteDeparture(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
            departureService.deleteDeparture(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    //    上传附件
    @RequestMapping("/uploadDeparture")
    public ResponseText upload(MultipartFile departure,@RequestParam String did) throws IOException {
        DepartureImg departureImg = new DepartureImg();
        try {
            DateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");
            String dateStr = dateFormat.format(new Date());
            File file1 = new File(addResourceLocation+"departure."+dateStr);
            if(!file1.exists()){
                file1.mkdirs();
            }
            String path=addResourceLocation+"departure."+dateStr;
            String fileName=UUID.randomUUID()+departure.getOriginalFilename();
            File file = new File(path,fileName);
            departure.transferTo(file);
//            获取地址存入数据库
            String netPath="http://localhost:8080/images/departure."+dateStr+"/"+fileName;
            departureImg.setDid(Integer.parseInt(did));
            departureImg.setImg(netPath);
            departureService.insertDepartureImg(departureImg);
            return new ResponseText(file.getPath(),200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
    }

    //删除离职图片信息
    @DeleteMapping("/departureImg/{id}")
    public ResponseText deleteMobilizeImg(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
            departureService.deleteDepartureImg(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
